<?php
$need_install = true;
include('inc/prepend.php');
$titre_page = 'Inscription';
if(isset($_POST['register'])) {
	$p = $_POST;
	$errors = '';
	if(intval($p['verif']) != intval($_SESSION['verif_code'])) {
		$errors .= 'Le code de validation n\'est pas valide<br/>';
	}
	if(empty($p['name']) || empty($p['repassword']) || empty($p['password'])) {
		$errors .= 'Tous les champs sont obligatoires<br/>';
	}
	if($p['password'] != $p['repassword']) {
		$errors .= 'Les deux mots de passe ne correspondent pas<br/>';
	}
	if(get_member_id($p['name']) >= 0) {
		$errors .= 'Cet utilisateur existe déjà<br/>';
	}
	if(empty($errors)) {
		$mdp = sha1($p['password']);
		$name = str_replace(array("\0", "\n"), '', trim($p['name']));
		$var = $Configuration->add_variable();
		$var->set_name('admin');
		$var->set_value($name);
		$Configuration->save(DATA_DIR.'configuration.ds');
		$user = $Utilisateurs->add_utilisateur();
		$user->set_pseudo($name);
		$user->set_password($mdp);
		$Utilisateurs->save(DATA_DIR.'utilisateurs.ds');
		$Sondages->save(DATA_DIR.'sondages.ds');
		$_SESSION['phppoll_connect'] = $name;
		redirect('index.php');
	}
}
?>
<h2>Inscription</h2>
<form method="post" action="register.php">
	<?php
	if(!empty($errors)) {
		echo '<p style="color: red;">Erreurs :<br/>'.$errors.'Réessayez !</p>';
	}
	?>
	<label for="name">Nom d'utilisateur</label>
	<input type="text" name="name" value="<?php echo $_POST['name']; ?>"/><br/>
	<label for="password">Mot de passe</label>
	<input type="password" name="password"/><br/>
	<label for="repassword">Répétez le mot de passe</label>
	<input type="password" name="repassword"/><br/>
	<label for="verif">Recopiez le code de validation</label>
	<img src="captcha/index.php" alt="captcha" /> <input type="text" name="verif"/><br/>
	<input type="submit" value="Inscription" name="register"/>
</form>
<?php
include(INC_DIR.'template.php');
?> 